﻿using System;
using System.Camera.Broadcast;
using System.Reflection;
using System.Windows.Forms;

namespace ViewCamHost
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the client form.
        /// </summary>
        /// <value>
        /// The client form.
        /// </value>
        public ClientForm clientForm { get; set; }

        /// <summary>
        /// Handles the Click event of the btnOpenFldr control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnOpenFldr_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldrBrowser = new FolderBrowserDialog();
            txtFolderPath.Text = GetNetworkFolder(fldrBrowser);
        }

        /// <summary>
        /// Gets the network folder.
        /// </summary>
        /// <param name="fldrBrowser">The FLDR browser.</param>
        /// <returns></returns>
        private string GetNetworkFolder(FolderBrowserDialog fldrBrowser)
        {
            Type type = fldrBrowser.GetType();
            FieldInfo fieldinfo = type.GetField("rootFolder", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldinfo.SetValue(fldrBrowser, (Environment.SpecialFolder)18);

            if (fldrBrowser.ShowDialog() == DialogResult.OK)
            {
                return fldrBrowser.SelectedPath;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            clientForm.folderPath = txtFolderPath.Text;
            this.Close();
        }
    }
}
